package com.buba.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.multipart.support.StandardServletMultipartResolver;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

/**
 * spring IOC/DI类
 */
@Configuration
public class SpringConfig {

    @Bean
    public StandardServletMultipartResolver getStandardServletMultipartResolver() {
        return new StandardServletMultipartResolver();
    }

    /**
     * jedis连接池
     */
    @Bean
    public JedisPoolConfig getJedisPoolConfig(){
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        // 同一时刻可分配最大连接数 默认值8 设置为负数时不做限制
        jedisPoolConfig.setMaxTotal(8);
        //最大空闲连接 默认8个 超出连接将被释放
        jedisPoolConfig.setMaxIdle(8);
        //请求连接最大等待时间(毫秒)，默认值 无限期 ，超出时间将抛出异常
        jedisPoolConfig.setMaxWaitMillis(3000);
        return jedisPoolConfig;
    }
    /**
     * jedis集群配置
     */
    @Bean
    public JedisCluster getJedisCluster(){
        Set<HostAndPort> set = new HashSet<>();
        set.add(new HostAndPort("192.168.157.128",6379));
        set.add(new HostAndPort("192.168.157.128",6380));
        set.add(new HostAndPort("192.168.157.128",6381));
        set.add(new HostAndPort("192.168.157.128",6382));
        return new JedisCluster(set,getJedisPoolConfig());
    }

}
